﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGen.TS.Generation.Generators
{
     public class KnockoutObservableGenerator : ICodeGenerator
    {
         public BaseTypeMode BaseTypeMode
         {
             get;
             set;
         }

        public string OutputFileName
        {
            get;
            set;
        }

        public string ModulName
        {
            get;
            set;
        }

        public string NewTypeName
        {
            get;
            set;
        }

        public bool UseObservable
        {
            get { return true; }
            set { }//Is pointless here
        }

        public int RecursionLevel
        {
            get;
            set;
        }

        private Type type;
        public Type Type
        {
            get { return type; }
            set
            {
                type = value;

                if (NewTypeName == null)
                {
                    NewTypeName = "KnockoutObservable" + (type.Namespace ?? "").Replace(".", "") + "I" + type.Name;
                }
            }
        }

        public string BaseIntend
        {
            get;
            set;
        }


        public string GenerateOutput()
        {


            var generator = Output.TypeGeneratorFactory.GetGenerator(Type, false);
            if (generator == null)
            {
                ModulName = null;
                NewTypeName = "KnockoutObservableAny";
                return "";//no code from here required
            }
            else
            {
                string definition = Properties.Settings.Default.KnockoutJSObservableDefinition;
                definition = definition.Replace("@@ModuleName@@", ModulName);
                definition = definition.Replace("@@TypeName@@", NewTypeName);
                definition =definition.Replace("@@Type@@", (generator.ModulName==null?"": generator.ModulName+".") + generator.NewTypeName);
                string output = BaseIntend + definition.Replace("\r\n", "\r\n" + BaseIntend).Replace("\r", BaseIntend + "\r").Replace("\n", "\n" + BaseIntend);
                return output;
            }
            
        }

        private bool isPreparedForOutput=false;
        public bool IsPreparedForOutput
        {
            get { return isPreparedForOutput; } 
        }

        public void PrepareForOutput(bool allowRecursion)
        {
           // if (allowRecursion) We ignore allowRecursion as recursion is required because chaning the name later will not work anymore
            {
                var generator = Output.TypeGeneratorFactory.GetGenerator(Type, false);
                if (generator == null)
                {
                    generator = new InterfaceGenerator();
                    generator.Type = this.Type;
                    generator.BaseTypeMode = this.BaseTypeMode;
                    generator.ModulName = Type.Namespace;
                    generator.NewTypeName = "I" + Type.Name;

                    this.CopyToDependend(generator, false);
                    generator.UseObservable = false;
                    Output.TypeGeneratorFactory.RegisterGenerator(generator);
                }
            }
            
            isPreparedForOutput = true;
        }

        
    }
}
